package fsoft.com.vn.directory.dao;

import java.util.List;

import fsoft.com.vn.directory.form.Contact;

import org.hibernate.Session;
import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class ContactDAOImpl implements ContactDAO {

	@Autowired
	private SessionFactory sessionFactory;
    private Session session;

	//public void addContact(Contact contact) {
	//	sessionFactory.getCurrentSession().save(contact);
	//}
    @SuppressWarnings("unchecked")
	public List<Contact> listContact() {

		return sessionFactory.getCurrentSession().createQuery("from Contact")
				.list();
	}

	//public void removeContact(Integer id) {
	//	Contact contact = (Contact) sessionFactory.getCurrentSession().load(
	//			Contact.class, id);
	//	if (null != contact) {
	//		sessionFactory.getCurrentSession().delete(contact);
	//	}

	//}
    
    @SuppressWarnings("unchecked")
	public List<Contact> search(String Firstname, String Surname){
		session = sessionFactory.openSession();
		Criteria cr = session.createCriteria(Contact.class);
		cr.add(Restrictions.like("firstname", "%" + Firstname + "%"));
		cr.add(Restrictions.like("surname", "%" + Surname + "%"));
		return cr.list();
	}
}
